package com.gw2.gw2world;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.gw2.gw2world.controller.DatabaseOpenHelper;
import com.gw2.gw2world.controller.EventDownloader;
import com.gw2.gw2world.controller.Tools;
import com.gw2.gw2world.model.Event;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DisabledActiveEventsActivity extends Activity implements OnClickListener {
	private LinearLayout ll_events;
	private Button b_ok;
	private TextView tv_disabled, tv_bugged;
	private List<Event> events;
	private SharedPreferences prefs;
	private SharedPreferences.Editor editor;
	private EventDownloader downloader;
	private SparseArray<String> map_names;
	private DatabaseOpenHelper dbHelper;
	protected SQLiteDatabase db;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Tools.forceLocale(this);
		setContentView(R.layout.disabled_active_events);
		dbHelper = new DatabaseOpenHelper(this, "database", null, 1);
		db = dbHelper.getReadableDatabase();
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		downloader=new EventDownloader(this,prefs.getString("language", "en"),prefs.getInt("home_server", 0),prefs.getInt("guest1_server", 0),prefs.getInt("guest2_server", 0),false);
		Cursor c = db.query("location", null, "lang=?", new String[] { prefs.getString("language", "en") }, null,null, "min_level,max_level");
		map_names=new SparseArray<String>(c.getCount());
		events=new ArrayList<Event>(100);
		while (c.moveToNext())
			map_names.put(c.getInt(0), c.getString(2));
		ll_events=(LinearLayout)findViewById(R.id.ll_ae_disabled);
		tv_bugged = (TextView)findViewById(R.id.tv_ae_bugged);
		tv_disabled=(TextView)findViewById(R.id.tv_ae_disabled);
		b_ok=(Button)findViewById(R.id.b_ae_remove);
		b_ok.setOnClickListener(this);
		refreshList();
	}
	
	private void refreshList() {
		events.clear();
		ll_events.removeAllViews();
		//reload prefs
		for(String id : TextUtils.split(prefs.getString("ignored_events", ""),","))
			events.add(downloader.getEventFromId(id));
		for(String id : TextUtils.split(prefs.getString("bugged_events", ""),",")) {
			Event e=downloader.getEventFromId(id);
			e.bugged=true;
			events.add(e);
		}
		if(events.size()==0) {
			Toast.makeText(this, R.string.msg_no_more_events, Toast.LENGTH_SHORT).show();
			super.finish();
			return;
		}
		Collections.sort(events, Event.SORT_BY_MAP);
		//update UI
		tv_disabled.setText(String.valueOf(TextUtils.split(prefs.getString("ignored_events", ""),",").length));
		tv_bugged.setText(String.valueOf(TextUtils.split(prefs.getString("bugged_events", ""),",").length));
		int map=0;
		TextView map_header;
		CheckBox cb;
		for(Event e : events) {
			if(e.map_id!=map) {
				map_header=new TextView(this);
				map_header.setText(map_names.get(e.map_id));
				map_header.setBackgroundColor(Color.GRAY);
				ll_events.addView(map_header);
				map=e.map_id;
			}
			cb=new CheckBox(this);
			cb.setText(e.name);
			cb.setTag(e);
			if(e.group) cb.setCompoundDrawables(getResources().getDrawable(R.drawable.group), null, null, null);
			ll_events.addView(cb);
		}
	}

	@Override
	public void onClick(View button) {
		Event e;
		for(int i=0; i<ll_events.getChildCount(); i++) {
			View v=ll_events.getChildAt(i);
			if(v.getClass()==CheckBox.class)
				if(((CheckBox)v).isChecked()) {
					e=(Event)v.getTag();
					events.remove(e);
				}
		}
		List<Event> bugged=new ArrayList<Event>(), ignored=new ArrayList<Event>();
		for(Event ev : events) {
			if(ev.bugged) bugged.add(ev);
			else ignored.add(ev);
		}
		editor=prefs.edit();
		editor.putString("ignored_events", TextUtils.join(",", ignored));
		editor.putString("bugged_events", TextUtils.join(",", bugged));
		editor.commit();
		refreshList();
	}

}
